//
//  GoodViewController.m
//  Menu
//
//  Created by Lynn-Shin on 15/7/3.
//  Copyright (c) 2015年 lijiangang. All rights reserved.
//

#import "GoodViewController.h"

@interface GoodViewController ()

@end

@implementation GoodViewController

-(void)dealloc{
    
    [_goodCollectV release];
    [_allGoodArray release];
    [_HUD release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.view.backgroundColor = [UIColor lightGrayColor];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(125*WIDTH, 130*HEIGHT);
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 0, 10, 0);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    
    
    self.goodCollectV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, 375*WIDTH, 667*HEIGHT-64-49) collectionViewLayout:flowLayout];
    self.goodCollectV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.goodCollectV];
    [flowLayout release];
    [self.goodCollectV release];
    
    self.goodCollectV.delegate = self;
    self.goodCollectV.dataSource = self;
    
    [self.goodCollectV registerClass:[GoodCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"loading";
    [self.HUD show:YES];
    
    self.allGoodArray = [NSMutableArray array];
    
    [[DataHandle shareData] openDataBase];
//    [[DataHandle shareData] dropGoodCacheTable];
    [[DataHandle shareData] createGoodCacheTable];
    self.allGoodArray = [[DataHandle shareData] selectGoodCacheTable];
    self.isOK = YES;
    for (int i = 1; i < 3; i++) {
        
        [self downLoadData:i];
    }

    [self.goodCollectV reloadData];
    // Do any additional setup after loading the view.
}

-(void)downLoadData:(NSInteger )page{
    
    NSString *get = [NSString stringWithFormat:@"http://121.41.84.91:80/HandheldKitchen/api/found/tblBoutique!getTblBoutiqueTypeList.do?is_traditional=0&page=%ld&pageRecord=12&phonetype=1",page];
    
    [AFNTool get:get Body:nil HttpResult:JSON success:^(id result) {
        
        if (self.isOK == YES) {
            [self.allGoodArray removeAllObjects];
            [[DataHandle shareData] dropGoodCacheTable];
            [[DataHandle shareData] createGoodCacheTable];
        }
        NSMutableArray *array = [result objectForKey:@"data"];
        for (NSMutableDictionary *dic in array) {
            
            GoodModel *good = [[GoodModel alloc] initWithDic:dic];
            [self.allGoodArray addObject:good];
            [[DataHandle shareData] insertGoodCacheTable:good];
            [good release];
        }
        [self.goodCollectV reloadData];
        self.isOK = NO;
        self.netOK = YES;
        [self.HUD hide:YES];
    } failure:^(NSError *error) {
        
        self.HUD.labelText = @"加载失败";
        [self.HUD hide:YES afterDelay:1];

    }];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.allGoodArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    GoodCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor whiteColor];
    cell.goodModel = [self.allGoodArray objectAtIndex:indexPath.item];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (!self.netOK) {
        
        self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        self.HUD.labelText = @"请求失败";
        [self.HUD show:YES];
        [self.HUD hide:YES afterDelay:0.5];
        return;
    }
    GoodListViewController *goodListVC = [[GoodListViewController alloc] init];
    GoodModel *goodModel = [self.allGoodArray objectAtIndex:indexPath.item];
    goodListVC.navigationItem.title = goodModel.name;
    goodListVC.typeId = [goodModel.typeId integerValue];;
    
    [self.navigationController pushViewController:goodListVC animated:YES];
    [goodListVC release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
